package my.edu.model.User.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import my.edu.common.domain.Response;
import my.edu.model.User.entity.Permission;
import my.edu.model.User.entity.PermissionMenu;
import my.edu.model.User.entity.VO.MenuItem;
import my.edu.model.User.entity.VO.MenuItemVO;
import my.edu.model.User.service.IPermissionMenuService;
import my.edu.model.User.service.IPermissionService;
import my.edu.model.information.entity.VO.CustomerCategoryVO;
import my.edu.security.userEntity.UserDetail;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/function")
@Tag(name = "基础页面接口")
public class LayOutController {

    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IPermissionMenuService permissionMenuService;

    @PostMapping("/findMenuByPNumber")
    @Operation(summary = "获取目录")
    public List<MenuItemVO> findMenuByPNumber() {
        UserDetail user = (UserDetail) (SecurityUtils.getSubject().getPrincipal());
        Long userId = user.getId();
        List<Long> permissionList;
        if (user.getUsername().equals("admin")){
            permissionList =   permissionService.list().stream().map(Permission::getId).toList();
        }else {
            permissionList = permissionService.getByUserId(userId).stream().map(Permission::getId).toList();
        }
       List<MenuItem> menuItems= permissionMenuService.getByPermissionIds(permissionList).stream().map(i->BeanUtil.copyProperties(i, MenuItem.class)).toList();
        List<MenuItemVO> res = getMenuItemVOS(menuItems);
        return res;
    }


    @PostMapping("/findMenuByPNumberAll")
    @RequiresPermissions("system:config")
    @Operation(summary = "获取所有目录")
    public Response findMenu() {
        List<MenuItem> menuItems= permissionMenuService.list().stream().map(i->BeanUtil.copyProperties(i, MenuItem.class)).toList();
        List<MenuItemVO> res = getMenuItemVOS(menuItems);
        return Response.success(res) ;
    }

    @PostMapping("/updateMenu")
    @RequiresPermissions("system:config")
    @Operation(summary = "修改目录顺序")
    public Response updateMenu(@RequestBody ArrayList<MenuItem> menuList) {
        permissionMenuService .updateMenuSort(menuList,0L);
        return Response.success();
    }


    private void buildChildren(MenuItem parent, List<MenuItem> allCategories) {
        List<MenuItem> children = new ArrayList<>();
        for (MenuItem category : allCategories) {
            if (category.getParentId() != null && category.getParentId().equals(parent.getId())) {
                buildChildren(category, allCategories);
                children.add(category);
            }
        }
        children.sort(Comparator.comparingInt(MenuItem::getSort));
        parent.setChildren(children);
    }

    private List<MenuItemVO> getMenuItemVOS(List<MenuItem> menuItems) {
        List<MenuItemVO> res=new ArrayList<>();
        for (MenuItem menu : menuItems) {
            if (menu.getParentId()==0){
                buildChildren(menu, menuItems);
                if (menu.getChildren().isEmpty()&&menu.getPermissionId()==0&&!menu.getText().equals("首页"))continue;
                res.add(BeanUtil.copyProperties(menu, MenuItemVO.class));
            }
        }
        res.sort(Comparator.comparingInt(MenuItemVO::getSort));
        return res;
    }



}
